
import { Component, ViewChild, Injector, Output, EventEmitter, ElementRef, ViewEncapsulation } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { AppComponentBase } from '@shared/common/app-component-base';
import { LocationServiceProxy, TechFileServiceProxy, LocationDto, TechFileListDto, TechFileBaseInputDto } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';

@Component({
    selector: 'app-putreceive-techfile-modal',
    templateUrl: './putreceive-techfile-modal.component.html',
    encapsulation: ViewEncapsulation.None,
    // styleUrls: ['./receive-techfile-modal.component.less']
})

export class PutReceiveTechfileModalComponent extends AppComponentBase {

    @ViewChild('actionModal') modal: ModalDirective;

    @ViewChild('areaCombobox') areaCombobox: ElementRef;
    @ViewChild('shelfCombobox') shelfCombobox: ElementRef;
    @ViewChild('positionCombobox') positionCombobox: ElementRef;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
  
    remarks:string;
    areas = new Array<LocationDto>();
    shelfs = new Array<LocationDto>();
    positions = new Array<LocationDto>();
    locations: any[];
    hasLoadLocations = false;

    areaId = 0;
    shelfId = 0;
    positionId = 0;
    isUpdate :boolean;

    selectedids:Array<string>;
    items = [
        { label: '数据校验' },
        { label: '位置选择' }
    ];

    constructor(
        injector: Injector,
        private _techFileServiceProxy: TechFileServiceProxy,
        private _locationServiceProxy: LocationServiceProxy,

    ) {
        super(injector);
    }

    show(bills: string[],type : boolean): void { 

        this.areaId = 0;
        this.shelfId = 0;
        this.positionId = 0;
        this.isUpdate =type;
        this.active = true;
        this.selectedids = bills; 
        bills=[];
        this._locationServiceProxy.getAreas()
            .subscribe(result => {
                this.areas = result.items;
                setTimeout(() => {
                    $(this.areaCombobox.nativeElement).selectpicker('refresh');
                    $(this.shelfCombobox.nativeElement).selectpicker('refresh');
                    $(this.positionCombobox.nativeElement).selectpicker('refresh');

                    this.modal.show();

                }, 0);
            });



    }


 

    loadShelfs() {
        this.shelfId = 0;
        this.positionId = 0;

        this._locationServiceProxy.getShelvesByArea(this.areaId)
            .subscribe(result => {
                this.shelfs = result.items;
                setTimeout(() => {
                    $(this.shelfCombobox.nativeElement).selectpicker('refresh');
                }, 0);
            });
    }

    loadPositions() {
        this.positionId = 0;

        this._locationServiceProxy.getPositionsByShelf(this.shelfId)
            .subscribe(result => {
                this.positions = result.items;
                setTimeout(() => {
                    $(this.positionCombobox.nativeElement).selectpicker('refresh');
                }, 0);
            });
    }

    save(): void {
        this.saving = true;

        let input = new TechFileBaseInputDto();
        input.ids = this.selectedids;
        input.positionId = this.positionId;
        input.remarks = this.remarks;
        this._techFileServiceProxy.receivePutTechFile(input)
            .pipe(finalize(() => {
                this.saving = false;
            }))
            .subscribe((result) => {
                this.close();
                this.modalSave.emit(null);
                this.notify.success('入库成功');
            });
    }

    update(): void {
        this.saving = true;

        let input = new TechFileBaseInputDto();
        input.ids = this.selectedids;
        input.positionId = this.positionId;
        input.remarks = this.remarks;
        this._techFileServiceProxy.updateTechFileLocationAll(input)
            .pipe(finalize(() => {
                this.saving = false;
            }))
            .subscribe((result) => {
                this.close();
                this.modalSave.emit(null);
                this.notify.success('修改成功');
            });
    }
    close(): void {
        this.saving = false;
        this.modal.hide();
    }
}
